
// ImageShowMFCView.cpp : implementation of the CImageShowMFCView class
//

#include "stdafx.h"
#include "ImageShowMFC.h"

#include "ImageShowMFCDoc.h"
#include "ImageShowMFCView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CImageShowMFCView

IMPLEMENT_DYNCREATE(CImageShowMFCView, CView)

BEGIN_MESSAGE_MAP(CImageShowMFCView, CView)
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, &CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, &CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, &CImageShowMFCView::OnFilePrintPreview)
	ON_WM_CREATE()
	ON_WM_SIZE()
	ON_WM_LBUTTONDOWN()
	ON_WM_RBUTTONDOWN()
	ON_WM_MOUSEMOVE()
	ON_WM_LBUTTONUP()
	ON_WM_RBUTTONUP()
END_MESSAGE_MAP()

// CImageShowMFCView construction/destruction

CImageShowMFCView::CImageShowMFCView()
{
	// TODO: add construction code here
	m_presenter = NULL;

	//m_zoom = new GraphicImageZoomController(m_presenter);
	flag = 0;

}

CImageShowMFCView::~CImageShowMFCView()
{
}
  
BOOL CImageShowMFCView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs
	//int w = cs.cx;
	//int h = cs.cy;
	//m_pDC = new CClientDC(this);
	//m_presenter = new GraphicImagePresenter((void*)m_pDC->GetSafeHdc(),786,378);

	//CImageShowMFCDoc* pDoc = GetDocument();
	//ASSERT_VALID(pDoc);
	//if (!pDoc)
	//	return false;

	//m_presenter->SetModel(pDoc->GetModel());

	return CView::PreCreateWindow(cs);
}

// CImageShowMFCView drawing

void CImageShowMFCView::OnDraw(CDC* /*pDC*/)
{
	CImageShowMFCDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	if (!pDoc)
		return;

	// TODO: add draw code for native data here
	if(m_presenter == NULL) return;
  	m_presenter->Update();
}
    
// CImageShowMFCView printing 


void CImageShowMFCView::OnFilePrintPreview()
{
	AFXPrintPreview(this);
}

BOOL CImageShowMFCView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CImageShowMFCView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CImageShowMFCView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

//void CImageShowMFCView::OnRButtonUp(UINT nFlags, CPoint point)
//{
//	ClientToScreen(&point);
//	OnContextMenu(this, point);
//}

void CImageShowMFCView::OnContextMenu(CWnd* pWnd, CPoint point)
{
	theApp.GetContextMenuManager()->ShowPopupMenu(IDR_POPUP_EDIT, point.x, point.y, this, TRUE);
}


// CImageShowMFCView diagnostics

#ifdef _DEBUG
void CImageShowMFCView::AssertValid() const
{
	CView::AssertValid();
}

void CImageShowMFCView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CImageShowMFCDoc* CImageShowMFCView::GetDocument() const // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CImageShowMFCDoc)));
	return (CImageShowMFCDoc*)m_pDocument;
}
#endif //_DEBUG


// CImageShowMFCView message handlers

int CImageShowMFCView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CView::OnCreate(lpCreateStruct) == -1)
		return -1;

	// TODO:  Add your specialized creation code here
	CRect rect;


	//HWND wnd = this->GetSafeHwnd();

	m_presenter = new GraphicImagePresenter(m_hWnd, 1,1, "OpenGL");
	m_zoom = new GraphicImageZoomController(m_presenter);
	m_rot = new GraphicImageRotateController(m_presenter);

	CImageShowMFCDoc* pDoc = GetDocument();
	m_presenter->SetModel(pDoc->GetModel());
	int w = rect.right-rect.left;
	int h = rect.bottom-rect.top;

	
	return 0;
}

void CImageShowMFCView::OnSize(UINT nType, int cx, int cy)
{
	CView::OnSize(nType, cx, cy);
	m_presenter->GetRenderer()->GetContext()->OnSizeChanged(cx,cy);
	/*if(cx>0&& flag==0){
		flag = 1;
		CClientDC   * m_pDC = new CClientDC(this);
		
		
		CImageShowMFCDoc* pDoc = GetDocument();
		ASSERT_VALID(pDoc);
		if (!pDoc)
			return ;

		
 
	}*/

	// TODO: Add your message handler code here
	if(cx == 0 || cy == 0) return;
	 
} 
void CImageShowMFCView::OnLButtonDown(UINT nFlags, CPoint point)
{
	// TODO: Add your message handler code here and/or call default
	if(m_rot)
	{ 
		int pos[2] = {point.x,point.y};
		m_rot->OnMouseDown(pos);

	} 
	

	CView::OnLButtonDown(nFlags, point);
}
void CImageShowMFCView::OnRButtonDown(UINT nFlags, CPoint point)
{
	// TODO: Add your message handler code here and/or call default
	if(m_zoom)
	{ 
		int pos[2] = {point.x,point.y};
		m_zoom->OnMouseDown(pos);

	}  	 
  
	CView::OnRButtonDown(nFlags, point);
} 
   
 
void CImageShowMFCView::OnMouseMove(UINT nFlags, CPoint point)
{
	// TODO: Add your message handler code here and/or call default
    
	if(m_rot)
	{
		int pos[2] = {point.x,point.y};
		m_rot->OnMouseMove(pos);
		
	}
	if(m_zoom)
	{
		int pos[2] = {point.x,point.y};
		m_zoom->OnMouseMove(pos);

	}
	CView::OnMouseMove(nFlags, point);
}

void CImageShowMFCView::OnRButtonUp(UINT nFlags, CPoint point)
{
	// TODO: Add your message handler code here and/or call default
	if(m_zoom)
	{
		int pos[2] = {point.x,point.y};
		m_zoom->OnMouseUp(pos);

	}
	CView::OnRButtonUp(nFlags, point);
}
void CImageShowMFCView::OnLButtonUp(UINT nFlags, CPoint point)
{
	// TODO: Add your message handler code here and/or call default
	if(m_rot)
	{
		int pos[2] = {point.x,point.y};
		m_rot->OnMouseUp(pos);

	}
	
	CView::OnLButtonUp(nFlags, point);
}
